/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.fpt.c0810g.newspj.beans;

import com.aptech.fpt.c0810g.newspj.business.DanTriDataProcess;
import com.aptech.fpt.c0810g.newspj.dataaccess.NewsAction;
import com.aptech.fpt.c0810g.newspj.entity.News;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.w3c.dom.NodeList;

/**
 *
 * @author anhgt_b00501
 */
@ManagedBean
@ViewScoped
public class GetOneNews {

    private String url;
    private boolean avable = false;
    private News newsItem;
    private String message;
    private boolean flag = false;

    public boolean isFlag() {
        return flag;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public News getNewsItem() {
        return newsItem;
    }

    public void setNewsItem(News newsItem) {
        this.newsItem = newsItem;
    }

    public boolean isAvable() {
        return avable;
    }

    public void setAvable(boolean avable) {
        this.avable = avable;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    /** Creates a new instance of GetOneNews */
    public GetOneNews() {
    }

    public void doGetNews() {
        try {
            News news = new News();
            DanTriDataProcess instance = new DanTriDataProcess();
            NodeList nodeList = instance.getNodeList(this.url, "fl wid470");
            /*
             * Title :
             * nodeList.item(0).getChildNodes().item(1).getChildNodes().item(0).getChildNodes().item(5).getTextContent();
             * Sub content :
             * nodeList.item(0).getChildNodes().item(1).getChildNodes().item(0).getChildNodes().item(7).getTextContent();
             * Main content :
             * nodeList.item(0).getChildNodes().item(1).getChildNodes().item(0).getChildNodes().item(9).getTextContent();
             *
             */
            news.setTitle(nodeList.item(0).getChildNodes().item(1).getChildNodes().item(0).getChildNodes().item(5).getTextContent());
            news.setSubContent(nodeList.item(0).getChildNodes().item(1).getChildNodes().item(0).getChildNodes().item(7).getTextContent());
            news.setMainContent(nodeList.item(0).getChildNodes().item(1).getChildNodes().item(0).getChildNodes().item(9).getTextContent());
            news.setCatelogy(1);
            news.setSourceLink(this.url);
            news.setSourceID(1);
            news.setPhoto("");
            this.newsItem = news;
            this.avable = true;
        } catch (Exception e) {
            this.avable = false;
        }
    }

    public void insertNewsToDB() {
        ArrayList<News> news = new ArrayList<News>();
        news.add(newsItem);
        NewsAction newsAction = new NewsAction();
        try {
            boolean retuls = newsAction.insertNewsContent(news);
            if (retuls) {
                this.message = "Chú ý : Đã lấy tin thành công!";
            } else {
                this.message = "Chú ý : Lấy tin không thành công!";
            }
            this.flag = true;
        } catch (Exception e) {
            this.flag = false;
        }
    }
}
